package com.example.list;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.example.utils.ToastUtil;

public class EdittextPastActivity extends AppCompatActivity {
    private EditText etInput;
    private EditText etPast;
    private EditText etPastNo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edittext_past);
        etInput = (EditText) findViewById(R.id.et_input_value);
        etPast = (EditText) findViewById(R.id.et_past);
        etPastNo = (EditText) findViewById(R.id.et_past_no);
        etPast.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                etPast.requestFocus();
                //为方便用户操作，需要粘贴验证码功能
                //onTouch事件消费 return设为false，支持（显示）粘贴功能
                //设为true，关闭粘贴功能，
                return false;
            }
        });

        etPastNo.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                etPastNo.requestFocus();
                return true;
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void copy(View view) {
        String content = etInput.getText().toString();
        String result="";
        ClipboardManager clip = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
        if (TextUtils.isEmpty(content)) {
           result="666";
        }else {
            result=content;
        }
//        clip.setText(result);
        clip.setPrimaryClip(ClipData.newPlainText("label",result));

        ToastUtil.showToast("粘贴到的值："+result);
    }
}